﻿using Super_Management_Mayhem.Content.Spritesheet;
using Super_Management_Mayhem.Screens.Playing.ObjectInterfaces;
using Transformable_Engine_v2.Engine.Animation.ColorAnimation;
using Transformable_Engine_v2.Engine.Animation.PositionAnimation;
using Transformable_Engine_v2.Engine.Animation.RotationAnimation;
using Transformable_Engine_v2.Engine.Animation.ScaleAnimation;
using Transformable_Engine_v2.Engine.Animation.Trajectory;
using Transformable_Engine_v2.Engine.GameObjects.Containers;
using Transformable_Engine_v2.Engine.Graphics;
using Transformable_Engine_v2.Engine.Helper;
using Transformable_Engine_v2.Engine.Time;
using Transformable_Engine_v2.Engine.Transformable_Math.Geometry;

namespace Super_Management_Mayhem.Screens.Playing.Tiles
{
    public class Torch : Container, IOrderable
    {
        public static Container Red(TE_Vector2 positionGridBound)
        {
            return new Torch(new Color(137, 18, 0, 150), new Color(255, 84, 0, 150),
                             new Color(238, 255, 0), new Color(255, 0, 16)) { PositionGridBound = positionGridBound };
        }

        private readonly Container _smokeContainer;
        private readonly Container _sparkContainer;
         public Torch(Color smokeColor, Color smokeAlternateColor, Color sparkColor, Color sparkAlternateColor)
         {
             _smokeContainer = new Container(0.5f, 0.25f);
             _sparkContainer = new Container(0.5f, 0.25f);

             AddChildren(new ImageContainer
                             {
                                 Sprite = ItemSheet.GetSprite(ItemSheet.Tile.Torch)
                             });

             AddChildren(_smokeContainer, _sparkContainer);

             AddTimers(new Timer(25, true, () => SpawnSmoke(smokeColor, smokeAlternateColor)).AutoStart());
             AddTimers(new Timer(400, true, () => SpawnSpark(sparkColor, sparkAlternateColor)).AutoStart());
         }

        private void SpawnSpark(Color sparkColor, Color sparkAlternateColor)
        {
            var spark = new ImageContainer
                            {
                                Sprite = ParticleSheet.GetSprite(ParticleSheet.TorchFire.FireSpark),
                                Origin = TE_Rectangle.Alignment.CenterMiddle,
                                Color = sparkColor,
                                Scale = new TE_Vector2(0.5f, 0.5f)
                            };

            spark.AddAnimations(new PositionUniformLinearAnimation(0, new TE_Vector2(0, -1) * 1));
            spark.AddAnimations(new ColorAnimation(new SinusTrajectory(), sparkAlternateColor, 400, true, 0));
            spark.AddAnimations(new PositionAnimation(new SinusTrajectory(), new TE_Vector2(Randomizer.Float(-0.5f, 0.5f), 0), 500, true, 0));
            spark.AddAnimations(new ScaleAnimation(new LinearEasing(), TE_Vector2.Zero, 1000, false, 0));
            spark.PositionGridBound = new TE_Vector2(Randomizer.Float(-0.25f, 0.25f), Randomizer.Float(0.125f, 0.25f));
            spark.Die(1000);
            _sparkContainer.AddChildren(spark);
        }

        private void SpawnSmoke(Color smokeColor, Color smokeAlternateColor)
        {
            float colorLightness = Randomizer.Float(0, 1);

            var smoke = new ImageContainer
                            {
                                PositionGridBound = new TE_Vector2(Randomizer.Float(-0.125f, 0.125f), 0.125f),
                                Sprite = ParticleSheet.GetSprite((ParticleSheet.TorchFire)Randomizer.Int(1, 3)),
                                Origin = TE_Rectangle.Alignment.CenterMiddle,
                                Scale = new TE_Vector2(1, 1),
                                Color = smokeColor * colorLightness + smokeAlternateColor * (1 - colorLightness)
                            };
            smoke.AddAnimations(new ScaleAnimation(new LinearEasing(), TE_Vector2.Zero, 500, false, 0));
            smoke.AddAnimations(new PositionUniformLinearAnimation(0, new TE_Vector2(0, -1) * 2.5f));
            smoke.AddAnimations(new RotationUniformLinearAnimation(0, Randomizer.Double()));
            smoke.Die(500);
            _smokeContainer.AddChildren(smoke);
        }

        public float GetOrderingFactor()
        {
            return AbsolutePosition.Y;
        }
    }
}